package com.biyu.monitormanage.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 15491
 */
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class ThreadPoolConfig {

    private static final Logger logger = LoggerFactory.getLogger(ThreadPoolConfig.class);

    @Bean
    public ThreadPoolExecutor asyncServiceExecutor(ThreadPoolConfigProperties threadProperty) {
        logger.info("ThreadPoolExecutor is start");
        return new ThreadPoolExecutor(threadProperty.getCoreThreadNumber(),
                threadProperty.getMaxThreadNumber(),
                threadProperty.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(2999),
                new BizThreadFactory("PoliceForMonitorThreadPool---- "),
                new ThreadPoolExecutor.DiscardPolicy());
    }
}
